package org.chnxi.study.messagequeue.rockermq.producer;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

/**
 * 批量发送消息，使用的是异步的方式，无法知道发送的结果
 */
public class BatchMsgProducer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //使用默认提供的Producer
        DefaultMQProducer producer = new DefaultMQProducer();
        //设置NameServer地址
        producer.setNamesrvAddr("192.168.50.227:9876;192.168.50.227:9877");
        //启动Producer
        producer.start();

        List<Message> messages = new ArrayList<>();

        //循环10次模拟组装多条消息的集合
        for (int i=0; i<10; i++){
            //组装消息对象，设置topic、tag、msgCont，并获取发送结果
            Message msg = new Message("topicName" , "Tag" , (i+"_MessageContent").getBytes());
            messages.add(msg);
        }
        //批量发送10条消息
        producer.send(messages);
    }
}
